package com.example.smartcity1.news;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.example.smartcity1.R;
import com.example.smartcity1.adapter.NewsCommentsAdapter;
import com.example.smartcity1.bean.NewsCommentsBean;
import com.example.smartcity1.network.AppService;
import com.example.smartcity1.network.RetrofitClient;

import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.example.smartcity1.network.RetrofitClient.IP;

public class NewsCommentsActivity extends AppCompatActivity {

    private static final String TAG = "NewsCommentsActivity";
    ListView listView;
    private EditText editText;
    private Button button;
    private int id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_comments);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setTitle("评论");
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 1);
        Log.d(TAG, "onResponse: " + id);

        initView();
        initListData();

        button.setOnClickListener(v -> {
            String text = editText.getText().toString();
        });
    }

    private void initView() {
        listView = findViewById(R.id.listView);
        editText = findViewById(R.id.editText);
        button = findViewById(R.id.button);
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return super.onSupportNavigateUp();
    }

    private void initListData() {
        OkHttpClient client = new OkHttpClient.Builder().
                connectTimeout(60, TimeUnit.SECONDS).
                readTimeout(60, TimeUnit.SECONDS).
                writeTimeout(60, TimeUnit.SECONDS).build();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(IP)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        retrofit.create(AppService.class).getNewsCommentsData(id).enqueue(new Callback<NewsCommentsBean>() {
            @Override
            public void onResponse(Call<NewsCommentsBean> call, Response<NewsCommentsBean> response) {
                NewsCommentsBean bean = response.body();
                if (bean != null && bean.getCode() == 200) {
                    List<NewsCommentsBean.RowsDTO> list = bean.getRows();
                    NewsCommentsAdapter adapter = new NewsCommentsAdapter(NewsCommentsActivity.this, R.layout.item_news_comments, list);
                    listView.setAdapter(adapter);
                }
            }

            @Override
            public void onFailure(Call<NewsCommentsBean> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t.getLocalizedMessage());
            }
        });
    }
}